﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Christoc.Modules.CouponsDiscounts.Components;
using Christoc.Modules.CouponsDiscounts.Components.Controllers;

namespace Christoc.Modules.CouponsDiscounts
{
    public partial class DetailsBuy : CouponsDiscountsModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int idProduct = Convert.ToInt32(Request.QueryString["idProduct"]);
            ProductSale product = new ProductSale(ProductController.GetProduct(idProduct));
            _name.Text = product.Name;
            _price.Text = product.PriceReal.ToString();
            _priceDiscount.Text = product.PriceDiscountUnit.ToString();
        }

        protected void _buy_Click(object sender, EventArgs e)
        {
            int productId = Convert.ToInt32(Request.QueryString["idProduct"]);
            ProductSale product = new ProductSale(ProductController.GetProduct(productId));
            int DiscountId = product.DiscountId;

            if (_couponCode.Text != null && _couponCode.Text != "")
            {
                int couponId = Convert.ToInt32(_couponCode.Text);
                product.ApplyCoupon(couponId);
            }

            Transaction transaction = new Transaction();
            transaction.Price = product.PriceDiscountUnit;
            transaction.ProductId = productId;
            transaction.DiscountId = DiscountId;
            transaction.TransactionDate = DateTime.Now;

            TransactionController.Save(transaction);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Close", "<script type='text/javascript'>window.parent.dnnModal.closePopUp(false);</script>");
            //Response.Redirect("~/desktopmodules/buyproduct/View.ascx");
        }

        protected void _applyCoupon_Click(object sender, EventArgs e)
        {
            int idProduct = Convert.ToInt32(Request.QueryString["idProduct"]);
            ProductSale product = new ProductSale(ProductController.GetProduct(idProduct));
            if (product.ApplyCoupon(Convert.ToInt32(_couponCode.Text)))
            {
                _couponCode.Enabled = false;
            }
            _newPrice.Text = "Price of Buy: " + product.PriceDiscountUnit.ToString();
            _newPrice.Visible = true;
        }
    }
}